import router from '@/router/router.js'
import store from '@/store/index.js'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getStystemCode, getListFun } from '@/api/login/login'
// import axios from "axios";
// import getPageTitle from "@/util/get-page-title"; // no redirect whitelist
// import { Message } from "element-ui";
const whiteList = ['/login', '/auth-redirect']
// eslint-disable-next-line require-jsdoc
function getStystemCodeFun(host) {
  if (Object.keys(store.getters.systemInfo).length === 0) {
    return new Promise(resolve => {
      getStystemCode(host).then(res => {
        // console.log(res);
        if (res.data.code === 0) {
          const data = res.data.data
          resolve(data)
        } else {
          resolve({})
        }
      })
    })
  } else {
    return new Promise(resolve => {
      resolve(store.getters.systemInfo)
    })
  }
}

// eslint-disable-next-line require-jsdoc
// 获取单点的list
// eslint-disable-next-line require-jsdoc
function getList(params) {
  return new Promise(resolve => {
    getListFun(params).then(res => {
      // console.log(res);
      if (res.data.code === 0) {
        const data = res.data.data
        resolve(data)
      } else {
        resolve({})
      }
    })
  })
}

// 跳转前
router.beforeEach(async(to, from, next) => {
  NProgress.start()
  const host = window.location.host
  const data = await getStystemCodeFun(host)
  if (data !== null && Object.keys(data).length > 0) {
    store.commit('SET_SYSTEM_INFO', data)
  } else {
    this.$message({
      message: '系统信息获取失败!',
      type: 'error'
    })
    return
  }
  const code = data.productCode
  // let systemList = await getList();
  document.title = code === 'portal' ? '微服务平台' : '珠江投管'
  const token = to.query.token
  if (token) {
    store.commit('SET_ACCESS_TOKEN', token)
  }
  // 检查用户是否登录
  const hasToken = store.getters.access_token
  if (hasToken) {
    if (to.path === '/login') {
      next({ path: '/' })
    } else {
      // console.log(code);
      if (!store.getters.menu || store.getters.menu.length === 0) {
        await store.dispatch('GetMenu', code)
      }
      await store.dispatch('permission/generateRoutes', ['admin']) // 添加首页固定
      if (store.getters.project.projectName === null) {
        store.commit('menu/set_show_modal', true)
      }
      const targetPath = to.path
      if (store.getters.systemList.length === 0) {
        const params = {
          productCode: code,
          tenantId: store.getters.userInfo.tenantId
        }
        const systemList = await getList(params)
        store.commit('SET_SYS_AUTH_SYSTEM', systemList)
      }
      // 判断是否前往frame页面
      if (targetPath.indexOf('frame') > -1) {
        if (store.getters.project.projectName === null) {
          store.commit('menu/set_show_modal', true)
        } else {
          next()
        }
      } else if (targetPath.indexOf('open') > -1) {
        console.log(to)
        // 判断是否前往打开页面
        // 系统内部路由
        // 判断内部是否含有这个路由
        // console.log(to.path);
        // next();
        const url = to.fullPath.split('?')[1]
        const targetUrl = decodeURIComponent(url)
        window.open(`http://${targetUrl}`)
      } else {
        // 系统路由
        next()
      }
    }
    NProgress.done()
  } else {
    // 不区分是否是login的路径，会进入死循环
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      // console.log("xxxx");
      next({ path: '/login' })
      NProgress.done()
    }
  }
})
router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
// 跳转后
// router.afterEach((to, from) => {});
